package com.ltkj.shop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.shop.domain.ShopGoodsSpec;
import com.ltkj.shop.service.IShopGoodsSpecService;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.utils.poi.ExcelUtil;
import com.ltkj.common.core.web.page.TableDataInfo;

/**
 * 商品规格Controller
 * 
 * @author xupf
 * @date 2023-08-30
 */
@Api(tags = "商品规格")
@RestController
@RequestMapping("/goods/spec")
public class ShopGoodsSpecController extends BaseController
{
    @Autowired
    private IShopGoodsSpecService shopGoodsSpecService;

    /**
     * 查询商品规格列表
     */
    @ApiOperation("查询商品规格列表")
    @GetMapping("/list")
    public TableDataInfo list(ShopGoodsSpec shopGoodsSpec)
    {
        startPage();
        List<ShopGoodsSpec> list = shopGoodsSpecService.selectShopGoodsSpecList(shopGoodsSpec);
        return getDataTable(list);
    }

    /**
     * 查询商品规格列表
     */
    @ApiOperation("查询单个商品全部规格")
    @GetMapping("/listByGoodsId/{goodsId}")
    public AjaxResult list(@PathVariable("goodsId") Long goodsId)
    {
        if (null == goodsId) {
            error("请传入正确的商品ID！");
        }
        ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
        shopGoodsSpec.setGoodsId(goodsId);
        List<ShopGoodsSpec> list = shopGoodsSpecService.selectShopGoodsSpecList(shopGoodsSpec);
        return success(list);
    }

    /**
     * 导出商品规格列表
     */
    @ApiOperation("导出商品规格列表")
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopGoodsSpec shopGoodsSpec)
    {
        List<ShopGoodsSpec> list = shopGoodsSpecService.selectShopGoodsSpecList(shopGoodsSpec);
        ExcelUtil<ShopGoodsSpec> util = new ExcelUtil<ShopGoodsSpec>(ShopGoodsSpec.class);
        util.exportExcel(response, list, "商品规格数据");
    }

    /**
     * 获取商品规格详细信息
     */
    @ApiOperation("获取商品规格详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shopGoodsSpecService.selectShopGoodsSpecById(id));
    }

    /**
     * 新增商品规格
     */
    @ApiOperation("新增商品规格")
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopGoodsSpec shopGoodsSpec)
    {
        return toAjax(shopGoodsSpecService.insertShopGoodsSpec(shopGoodsSpec));
    }

    /**
     * 修改商品规格
     */
    @ApiOperation("修改商品规格")
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopGoodsSpec shopGoodsSpec)
    {
        return toAjax(shopGoodsSpecService.updateShopGoodsSpec(shopGoodsSpec));
    }

    /**
     * 删除商品规格
     */
    @ApiOperation("删除商品规格")
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopGoodsSpecService.deleteShopGoodsSpecByIds(ids));
    }
}
